using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;

public partial class Admin_Ad_MngResources : System.Web.UI.Page
{
    Hashtable htSelectedItems;

    protected void Page_Load(object sender, EventArgs e)
    {
        //BEGIN : STORE CHECK STATUS THROUGH-OUT PAGING
        if (ViewState["Indexer"] == null)
        {
            htSelectedItems = new Hashtable();
            ViewState["Indexer"] = htSelectedItems;
        }
        else
        {
            htSelectedItems = new Hashtable();
            htSelectedItems = (Hashtable)ViewState["Indexer"];
        }
        //END   : STORE CHECK STATUS THROUGH-OUT PAGING                

        if (!IsPostBack)
        {            
            BindGrid();
        }   //not a post back ends 
        
    }    

    protected void grdResources_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox chk;

        chk = (CheckBox)e.Row.FindControl("chkDelete");
        
        if (chk!=null)
        {
            if (htSelectedItems.ContainsKey(e.Row.Cells[2].Text.Trim()))
            {
                chk.Checked = true;

                if (Request.QueryString["cmd"] != null)
                {

                    if (Request.QueryString["cmd"].ToUpper() == "A")
                    {
                        LinkButton lnk = new LinkButton();
                        lnk.Text = e.Row.Cells[3].Text;
                        lnk.Attributes.Add("onclick", "window.open('" + ((lnk.Text.IndexOf("http://") > 0) ? lnk.Text : "http://" + lnk.Text) + "'); return false;"); 
                        e.Row.Cells[3].Controls.Add(lnk);
                        lnk = null;
                    }
                    else if (Request.QueryString["cmd"].ToUpper() == "B")
                    {
                        Label lbl = new Label();
                        lbl.Text = e.Row.Cells[3].Text;
                        e.Row.Cells[3].Controls.Add(lbl);
                        lbl = null;
                    }
                }
            }
            else
            {
                chk.Checked = false;

                if (Request.QueryString["cmd"] != null)
                {

                    if (Request.QueryString["cmd"].ToUpper() == "A")
                    {
                        LinkButton lnk = new LinkButton();
                        lnk.Text = e.Row.Cells[3].Text;
                        lnk.Attributes.Add("onclick", "window.open('" + ((lnk.Text.IndexOf(@"http://") == 0) ? lnk.Text : @"http://" + lnk.Text) + "');return false;"); 
                        e.Row.Cells[3].Controls.Add(lnk);
                        lnk = null;
                    }
                    else if (Request.QueryString["cmd"].ToUpper() == "B")
                    {
                        Label lbl = new Label();
                        lbl.Text = e.Row.Cells[3].Text;
                        e.Row.Cells[3].Controls.Add(lbl);
                        lbl = null;
                    }
                }
            }
        }

        
        return;
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        SaveChecks();

        string strID = string.Empty;
        foreach(DictionaryEntry de in htSelectedItems)
        {
            strID += de.Key.ToString() + ",";        
        }
        if (strID.Length > 0)
        {
            strID = strID.Substring(0, strID.LastIndexOf(','));
            Manager mngr = new Manager(true,"ConnectionString");
            mngr.ExecuteUpdate("UPDATE TBL_LINKTOUS SET ISDELETE=1 WHERE SNO IN (" + strID + ");");
            
            BindGrid();

            mngr = null;

            lblMsg.ForeColor = System.Drawing.Color.DarkGreen;
            lblMsg.Text = "Selected resources have been deleted!";
        }
        else
        {
            lblMsg.ForeColor = System.Drawing.Color.Red;
            lblMsg.Text = "No resource are selected to delete!";
        }
    }
   
    protected void grdResources_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SaveChecks(); 
        
        grdResources.DataSource = (DataSet)ViewState["Data"];
        grdResources.PageIndex = e.NewPageIndex; 
        grdResources.DataBind();

        return;
    }

    /// <summary>
    /// Find check-box status and save it in View State
    /// </summary>
    /// <remarks>
    /// AUTHOR  : KAJAL MANDAL
    /// DATE    : 10 APRIL 2007
    /// </remarks>
    private void SaveChecks()
    {
        foreach (GridViewRow row in grdResources.Rows)
        {            
            if (((CheckBox)row.FindControl("chkDelete")).Checked)
            {
                if (!htSelectedItems.ContainsKey(row.Cells[2].Text))
                {
                    htSelectedItems.Add(row.Cells[2].Text, row.Cells[2].Text);
                }
            }
            else
            {
                if (htSelectedItems.ContainsKey(row.Cells[2].Text))
                {
                    htSelectedItems.Remove(row.Cells[2].Text);
                }
            }
        }

        ViewState["Indexer"] = htSelectedItems;
        return;
    }
    
    protected void lnkEdit_Click(object sender, EventArgs e)
    {        
        SaveChecks();
        LinkButton lnk = (LinkButton)sender;
        Session["link_id"] = lnk.CommandArgument;
        Response.Redirect(Session["strHttp"].ToString() + "admin/ad_addeditresources.aspx?cmd=e&id=" + Session["link_id"].ToString(), true);
        lnk = null;
        return;
    }

    /// <summary>
    /// Bind the grid as per the querystring.
    /// </summary>
    /// <remarks>
    /// AUTHOR  : KAJAL MANDAL
    /// DATE    : 19 APRIL 2007
    /// </remarks>
    private void BindGrid()
    {
        bool bInvalid = false;

        if (Request.QueryString.HasKeys())
        {
            if (Request.QueryString["cmd"] != null)
            {

                if (Request.QueryString["cmd"].ToUpper() == "A")
                {
                    lblHead.Text = "View/Edit Link(s)";

                    DataSet ds = new DataSet();
                    Manager mgr = new Manager(true,"ConnectionString");
                    ds = mgr.ExecuteQuery("SELECT SNo ,URL,DATE as [Date] FROM TBL_LINKTOUS WHERE ISDELETE=0 AND STATUS='A'", "TBL");

                    grdResources.DataSource = ds;
                    grdResources.DataBind();

                    ViewState["Data"] = ds;
                    ds = null;
                    mgr = null;
                }
                else if (Request.QueryString["cmd"].ToUpper() == "B")
                {
                    lblHead.Text = "Manage Link(s)";

                    DataSet ds = new DataSet();
                    Manager mgr = new Manager(true,"ConnectionString");
                    ds = mgr.ExecuteQuery("SELECT SNo,TITLE,CASE STATUS WHEN 'V' THEN 'REVIEW' WHEN 'A' THEN 'APPROVED' WHEN 'R' THEN 'REJECTED' END AS [Status], DATE as [Date] FROM TBL_LINKTOUS WHERE ISDELETE=0 AND (STATUS='V' or STATUS='R')", "TBL");

                    grdResources.DataSource = ds;
                    grdResources.DataBind();

                    ViewState["Data"] = ds;
                    ds = null;
                    mgr = null;
                }
                else
                {
                    bInvalid = true;
                }
            }
            else
            {
                bInvalid = true;
            }
        }
        else
        {
            bInvalid = false;
        }

        if (bInvalid) Response.Redirect(Session["Strhttp"].ToString() + "admin/Ad_logout.aspx", true);
        return;
    }
}
